package com.lfd.rocketmq.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @项目名： spring-emq
 * @包名： com.lfd.rocketmq.service
 * @文件名: OnewayRocletMq
 * @创建者: liufudong@szyh-smart.com
 * @创建时间: 2024/9/30 22:22
 * @描述：单向消息 生产者
 */
@Service
@Slf4j
public class OnewayProducerRocketMQ {
    @Autowired
    private ObjectMapper objectMapper;

    public void sendOnewayMsg(String namesrvAddr, String topic, String tag, String msg) throws MQClientException, MQBrokerException, RemotingException, InterruptedException, IOException {
        // 创建生产者并指定生产者组
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer(topic);
        defaultMQProducer.setNamesrvAddr(namesrvAddr);
        defaultMQProducer.start();
        for (int i = 0; i < 10; i++) {
            defaultMQProducer.sendOneway(new Message(topic, tag, msg.getBytes()));
        }
    }
}
